<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>弹框组件 | 北城韩雨</title><meta name="keywords" content="JavaScript"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="弹框组件">
<meta property="og:url" content="http://example.com/2020/03/29/Dialog/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="JavaScript">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f">
<meta property="article:published_time" content="2020-03-28T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-03T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/03/29/Dialog/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '弹框组件',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-04 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">弹框组件</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-03-28T16:00:00.000Z" title="发表于 2020-03-29 00:00:00">2020-03-29</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-03T16:00:00.000Z" title="更新于 2019-06-04 00:00:00">2019-06-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/">项目实战</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">7.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>33分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="弹框组件"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="弹框组件封装"><a href="#弹框组件封装" class="headerlink" title="弹框组件封装"></a>弹框组件封装</h1><h2 id="思想"><a href="#思想" class="headerlink" title="思想"></a>思想</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这里我们封装的是一个插件组件 也就是 这里面有样式 和 html 结构</span><br><span class="line"><span class="number">2.</span> 不同于我们的方法 直接调用即可 这里可能还支持我们的自定义样式</span><br></pre></td></tr></table></figure>
<h2 id="需要考虑的事情"><a href="#需要考虑的事情" class="headerlink" title="需要考虑的事情"></a>需要考虑的事情</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 实现一个插件组件(UI) 这就可能存在处理样式的问题</span><br><span class="line"><span class="number">2.</span> 在插件内部 创建元素的时候 把样式 基于行内的样式 写入进去 所有的HTML结构 都要基于 JS 创建 这种方式 调用很方便  但是不能提取公共样式的提取 以及 后期的修改 如果用户自定义样式的处理 是不能实现的</span><br><span class="line"><span class="number">3.</span> 第二种方案。就是 把我们的方案写如到样式表中 如果不期望用户自己导入样式 我们在JS中动态创建LIKN标签 不过这种方式 必须要联网 并且导入的样式资源最好部署到CDN上(否则导入速度慢)</span><br><span class="line"><span class="number">4.</span> 样式中 尽量不要出现图片 因为会导致别人在用你的插件的时候 出现样式错乱 如果一定要用 那么尽量用base64</span><br><span class="line"><span class="number">5.</span> JS的处理 这里我们基本上都是用基于变向对象思想 去封装 这种好处 插件可以创建实例 调用一次 就相当于创建一个实例 实例和实例之间 既存在私有的属性方法 也能调用共公共的方法 并且还可以把类当作对象 </span><br><span class="line"><span class="number">6.</span> 需要考虑的DOM处理 页面中有原始结构 我只是控制显示隐藏(性能会好一些 但是不能弹出多个框) </span><br><span class="line">所有的结构 都是基于JS 动态创建(性能会有所消耗 但是可以根据需求自己创建N个)</span><br><span class="line"><span class="number">7.</span> 封装插件组件的目的 就是为了复用 也是为了构建敏捷化开发平台的一个重要环节</span><br></pre></td></tr></table></figure>
<h2 id="插件支持的配置信息"><a href="#插件支持的配置信息" class="headerlink" title="插件支持的配置信息"></a>插件支持的配置信息</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 调用一次插件 创建一套 新的插件结构 关闭的时候 把这套结构移除掉</span><br><span class="line">+title[string]  插件的标题 可以撇值</span><br><span class="line">+template[string] 内容不确定(template) 自定义内容 或者模版字符串 拼接更丰富的内容	</span><br><span class="line">+buttons[array] 弹框底部的 自定义 按钮(组)「array」 [&#123;<span class="attr">text</span>:<span class="string">&#x27;确定&#x27;</span>,<span class="attr">click</span>:callback&#125;] 按钮点击的时候 做什么的 回调函数</span><br><span class="line">+modal[boolean] 遮罩层 显示于隐藏 默认是<span class="literal">true</span> 显示的</span><br><span class="line">+drag[boolean] 按住头部的时候 是否允许拖拽 默认是<span class="literal">false</span></span><br><span class="line">+opened[boolean] 方法一执行 弹出框 是打开还是隐藏 默认 <span class="literal">true</span></span><br><span class="line"><span class="number">2.</span> 配置生命周期函数 当前某个阶段 要额外做的事情 允许用户自定义处理的事情</span><br><span class="line">+ 打开的时候 onopen</span><br><span class="line">+ 关闭的时候 onclose</span><br><span class="line">+ 拖拽开始 ondragstart</span><br><span class="line">+ 拖拽中 ondraging</span><br><span class="line">+ 拖拽结束 ondragend</span><br></pre></td></tr></table></figure>
<h2 id="创建的方式"><a href="#创建的方式" class="headerlink" title="创建的方式"></a>创建的方式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们分两种</span><br><span class="line"><span class="number">2.</span> 第一种就是 直接 <span class="keyword">new</span> ModelPlugin 执行</span><br><span class="line"><span class="number">3.</span> 第二种就是 可以直接当函数执行 ModelPlugin 但是也返回他的实例</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第一种</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">ModalPlugin</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">  	 <span class="comment">//挂在到window上</span></span><br><span class="line">     <span class="built_in">window</span>.ModalPlugin = ModalPlugin</span><br><span class="line">&#125;)()</span><br><span class="line"><span class="comment">// 用的时候</span></span><br><span class="line"><span class="keyword">new</span> ModalPlugin()</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第二种 把原型中转一下 可以代理一下 直接使用函数调用的方式 就可以 创建实例</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">ModalPlugin</span>(<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Factory(options)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">Factory</span>(<span class="params">options</span>)</span>&#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    ModalPlugin.prototype = &#123;</span><br><span class="line">        <span class="title">constructor</span>:<span class="title">ModalPlugin</span>,</span><br><span class="line">        <span class="title">version</span>:&#x27;1.0.0&#x27;</span><br><span class="line">    &#125;</span><br><span class="line">  	// 重写<span class="title">Factory</span>原型指向</span><br><span class="line">    <span class="title">Factory</span>.<span class="title">prototype</span> = <span class="title">ModalPlugin</span>.<span class="title">prototype</span></span><br><span class="line">    //支持<span class="title">commonjs</span> 和 <span class="title">ES6</span> <span class="title">moudel</span> 规范</span><br><span class="line">    <span class="title">if</span>(<span class="params"><span class="keyword">typeof</span> <span class="built_in">window</span> !== <span class="string">&#x27;undefined&#x27;</span></span>)&#123;</span><br><span class="line">        <span class="built_in">window</span>.M = <span class="built_in">window</span>.ModalPlugin  = ModalPlugin</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">module</span> === <span class="string">&#x27;object&#x27;</span> &amp;&amp; <span class="keyword">typeof</span> <span class="built_in">module</span>.exports === <span class="string">&#x27;object&#x27;</span>)&#123;</span><br><span class="line">        <span class="built_in">module</span>.exports = ModalPlugin</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line"><span class="comment">//以后在用的的时候 直接当作函数调用</span></span><br><span class="line">M()</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 另外一种写法</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">ModalPlugin</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">    ModalPlugin.prototype = &#123;</span><br><span class="line">        <span class="title">constructor</span>:<span class="title">ModalPlugin</span>,</span><br><span class="line">        <span class="title">version</span>:&#x27;1.0.0&#x27;</span><br><span class="line">    &#125;</span><br><span class="line">    //为了既可以像函数一样调用  也可以创造实例</span><br><span class="line">    <span class="title">function</span> <span class="title">proxy</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ModalPlugin()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">window</span> !== <span class="string">&#x27;undefined&#x27;</span>)&#123;</span><br><span class="line">      <span class="comment">//在导出的时候 我们直接到处proxy</span></span><br><span class="line">        <span class="built_in">window</span>.M = <span class="built_in">window</span>.ModalPlugin  = proxy</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">module</span> === <span class="string">&#x27;object&#x27;</span> &amp;&amp; <span class="keyword">typeof</span> <span class="built_in">module</span>.exports === <span class="string">&#x27;object&#x27;</span>)&#123;</span><br><span class="line">        <span class="built_in">module</span>.exports = proxy</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line"><span class="comment">//以后在用的的时候 直接当作函数调用</span></span><br><span class="line">M()</span><br></pre></td></tr></table></figure>
<h2 id="参数传递"><a href="#参数传递" class="headerlink" title="参数传递"></a>参数传递</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 在需要传递很多实参的情况下(一般两个以上就认为多了) 如果是基于设定行参的方式来处理 就要存在严格的传递顺序 以及很难给参数设定默认是</span><br><span class="line"><span class="number">2.</span> 所以如果参数比较多。我们一般 都是基于对象 去传递 这样也比较好管理</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">proxy</span>(<span class="params"> options = &#123;&#125; </span>)</span>&#123;</span><br><span class="line">    <span class="comment">//1. 参数 我们现在基于对象传递 我们需要的参数有 title template buttons modal drag opened 这些字段</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">Object</span>.prototype.toString.call(options) !== <span class="string">&#x27;[object Object]&#x27;</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">&#x27;类型不对,需要一个对象&#x27;</span>,)</span><br><span class="line">    <span class="comment">//2. 参数处理 格式 校验 初始化默认值</span></span><br><span class="line">    options = <span class="built_in">Object</span>.assign(&#123;</span><br><span class="line">        title:<span class="string">&#x27;系统温馨提示&#x27;</span>,</span><br><span class="line">        template:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        buttons:[],</span><br><span class="line">        modal:<span class="literal">true</span>,</span><br><span class="line">        drag:<span class="literal">true</span>,</span><br><span class="line">        opened:<span class="literal">true</span></span><br><span class="line">    &#125;,options)</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> ModalPlugin()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. 校验参数类型 参数是传了 但是不是我们的要的类型 我们就要校验一下</span></span><br><span class="line">    <span class="keyword">const</span> toType = <span class="function"><span class="keyword">function</span> (<span class="params">value</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> class2type = &#123;&#125;</span><br><span class="line">        <span class="comment">//这里就是验证了你传进来的是什么类型 比如是 10 那么type 就是 [object Number]</span></span><br><span class="line">            type = class2type.toString.call(value)</span><br><span class="line">        <span class="comment">// 使用正则捕获到  把我们 [object Number] 中的 Number 截取出来 在转换成小写 </span></span><br><span class="line">        <span class="comment">// 这里是为了 跟我们的参数映射表 做匹配</span></span><br><span class="line">        <span class="keyword">return</span>  <span class="regexp">/^\[object ([a-z-A-Z]+)\]$/</span>.exec(type)[<span class="number">1</span>].toLowerCase()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//2.为了更严格的参数校验 我们就要建立参数映射表 为每一个参数严格校验</span></span><br><span class="line">    <span class="keyword">const</span> props = &#123;</span><br><span class="line">        title:&#123;</span><br><span class="line">            type:<span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:<span class="string">&#x27;系统温馨提示&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        template:&#123;</span><br><span class="line">            type:<span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">            required:<span class="literal">true</span></span><br><span class="line">        &#125;,</span><br><span class="line">        buttons:&#123;</span><br><span class="line">            type:<span class="string">&#x27;array&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:[]</span><br><span class="line">        &#125;,</span><br><span class="line">        modal:&#123;</span><br><span class="line">            type:<span class="string">&#x27;boolean&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:<span class="literal">true</span></span><br><span class="line">        &#125;,</span><br><span class="line">        drag:&#123;</span><br><span class="line">            type:<span class="string">&#x27;boolean&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:<span class="literal">true</span></span><br><span class="line">        &#125;,</span><br><span class="line">        opened:&#123;</span><br><span class="line">            type:<span class="string">&#x27;boolean&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:<span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">proxy</span>(<span class="params"> options = &#123;&#125; </span>)</span>&#123;</span><br><span class="line">    <span class="comment">//3. 参数 我们现在基于对象传递 我们需要的参数有 title template buttons modal drag opened 这些字段</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">Object</span>.prototype.toString.call(options) !== <span class="string">&#x27;[object Object]&#x27;</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">&#x27;类型不对,需要一个对象&#x27;</span>,)</span><br><span class="line">    <span class="comment">//4. 根据我们的参数映射规则表 校验参数</span></span><br><span class="line">    <span class="keyword">let</span> params = &#123;&#125;</span><br><span class="line">    <span class="built_in">Object</span>.keys(props).forEach( <span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">//5.拿到每项的验证规则</span></span><br><span class="line">        <span class="keyword">let</span> &#123;</span><br><span class="line">            type,<span class="attr">default</span>:defaultVal,required = <span class="literal">false</span></span><br><span class="line">        &#125; = props[key]</span><br><span class="line">        <span class="comment">//拿到 你传过来的参数 看看 你有没有传</span></span><br><span class="line">        <span class="keyword">let</span> val =  options[key]</span><br><span class="line">        <span class="comment">// 规则校验 这个是必须传的 但是 你没传 就报错</span></span><br><span class="line">        <span class="keyword">if</span>(required &amp;&amp; val == <span class="literal">undefined</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">`<span class="subst">$&#123;key&#125;</span> 必须传递`</span>)</span><br><span class="line">        <span class="comment">// 如果传了  但是不是我们要的类型 我们就需要类型校验</span></span><br><span class="line">        <span class="keyword">if</span>(val !== <span class="literal">undefined</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(toType(val) !== type) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">`<span class="subst">$&#123;key&#125;</span> 不是 <span class="subst">$&#123;type&#125;</span>`</span>)</span><br><span class="line">            <span class="comment">//如果参数类型也正确 那么我们就赋值</span></span><br><span class="line">            params[key] = val</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果没传 也不需要 类型校验 就用我们的默认值</span></span><br><span class="line">        params[key] = defaultVal</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> ModalPlugin()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="初始DOM结构"><a href="#初始DOM结构" class="headerlink" title="初始DOM结构"></a>初始DOM结构</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先 我们需要一个init 方法 这个方法 是初始化我们的 实例的</span><br><span class="line"><span class="number">2.</span> 需要一个创建DOM结构的方法</span><br><span class="line"><span class="number">3.</span> 这里 会根据传进来的参数 去决定 创建不创建遮罩层 中间的内容 怎么显示</span><br><span class="line"><span class="number">4.</span> 为了减少回流 这里使用文档碎片的方式</span><br><span class="line"><span class="number">5.</span> 而且这里为了方便使用 把我们的创建好的元素 都挂在我们实例本身上了</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 初始化 我们的实例 并且  并且初始一下 我们自己实例身上的属性</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ModalPlugin</span>(<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.options = options</span><br><span class="line">        <span class="comment">//1 需要用的东西都初始化一下</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_modal = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content = <span class="literal">null</span></span><br><span class="line">        <span class="comment">// 初始化结构  也就是创建 html结构</span></span><br><span class="line">        <span class="built_in">this</span>.init()</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 初始化我们的 DOM 结构 方法</span></span><br><span class="line">    ModalPlugin.prototype = &#123;</span><br><span class="line">        <span class="title">constructor</span>:<span class="title">ModalPlugin</span>,</span><br><span class="line">        <span class="title">version</span>:&#x27;1.0.0&#x27;,</span><br><span class="line">        //2.创建<span class="title">DOM</span>结构</span><br><span class="line">        <span class="title">createDOM</span>:<span class="title">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="comment">// 把我们的参数结构出来 用来 判断 你的哪些结构需要根据参数显示和隐藏</span></span><br><span class="line">            <span class="keyword">let</span> &#123;modal,title,template,buttons&#125; = <span class="built_in">this</span>.options</span><br><span class="line">            <span class="comment">// 这里我们注意 因为我们创建了两次元素 并且往body里插入了两次 就会产生两次回流 所以我们使用文档碎片</span></span><br><span class="line">            <span class="comment">// 先都插入到文档碎片里  然后 在想body里插入文档碎片</span></span><br><span class="line">            <span class="keyword">let</span> frag = <span class="built_in">document</span>.createDocumentFragment()</span><br><span class="line">            <span class="comment">// 判断用户传进来的 是否需要遮罩层</span></span><br><span class="line">            <span class="keyword">if</span>(modal)&#123;</span><br><span class="line">                <span class="comment">// 创建好遮罩层 因为可能这个遮罩层 需要显示和隐藏 所以我们要挂在到我们的实例上</span></span><br><span class="line">                <span class="built_in">this</span>.$drag_modal = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">                <span class="built_in">this</span>.$drag_modal.className = <span class="string">&#x27;drag_modal&#x27;</span></span><br><span class="line">                <span class="comment">// 把创建好的遮罩层 插入到我们的body中</span></span><br><span class="line">                frag.appendChild(<span class="built_in">this</span>.$drag_modal)</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 创建内容结构</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">            <span class="built_in">this</span>.$drag_content.className = <span class="string">&#x27;drag_content&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content.innerHTML = <span class="string">`</span></span><br><span class="line"><span class="string">            &lt;div class=&quot;drag_head&quot;&gt;</span></span><br><span class="line"><span class="string">                <span class="subst">$&#123;title&#125;</span></span></span><br><span class="line"><span class="string">                &lt;a href=&quot;javascript:;&quot; class=&quot;drag_close&quot;&gt;&lt;/a&gt;</span></span><br><span class="line"><span class="string">            &lt;/div&gt;</span></span><br><span class="line"><span class="string">            &lt;div class=&quot;drag_main&quot;&gt;</span></span><br><span class="line"><span class="string">                <span class="subst">$&#123;template&#125;</span></span></span><br><span class="line"><span class="string">            &lt;/div&gt;</span></span><br><span class="line"><span class="string">            <span class="subst">$&#123;buttons.length &gt; <span class="number">0</span>? <span class="string">`&lt;div class=&quot;drag_foot&quot;&gt;</span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">                <span class="subst">$&#123;buttons.map((item,index) =&gt; &#123;</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">                    <span class="keyword">return</span> <span class="string">`&lt;a href=&quot;javascript:;&quot; class=&quot;drag_button&quot;&gt;</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                                <span class="subst">$&#123;item.text&#125;</span></span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                            &lt;/a&gt;`</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">                &#125;).join(<span class="string">&#x27;&#x27;</span>)&#125;</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">            &lt;/div&gt;`</span>:<span class="string">&#x27;&#x27;</span>&#125;</span></span></span><br><span class="line"><span class="string">            `</span></span><br><span class="line">            <span class="comment">//使用文档碎片 减少回流</span></span><br><span class="line">            frag.appendChild(<span class="built_in">this</span>.$drag_content)</span><br><span class="line">            <span class="built_in">document</span>.body.appendChild(frag)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//7.1 入口 命令模式 主要初始化我们的结构</span></span><br><span class="line">        init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.createDOM()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="默认打开弹框"><a href="#默认打开弹框" class="headerlink" title="默认打开弹框"></a>默认打开弹框</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们 的弹框 组件 默认是不是 直接就打开的</span><br><span class="line"><span class="number">2.</span> 根据我们传进去的变量 opened 来控制</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">let</span> m1 = M(&#123;</span><br><span class="line">    template:<span class="string">&#x27;今天我完成了插件组件的封装&#x27;</span>,</span><br><span class="line">    model:<span class="literal">true</span>,</span><br><span class="line">    buttons:[</span><br><span class="line">        &#123;<span class="attr">text</span>:<span class="string">&#x27;确定&#x27;</span>&#125;,</span><br><span class="line">        &#123;<span class="attr">text</span>:<span class="string">&#x27;取消&#x27;</span>&#125;</span><br><span class="line">    ],</span><br><span class="line">    opened:<span class="literal">true</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.createDOM()</span><br><span class="line">            <span class="comment">// 默认就打开弹框和遮罩层 通过你传进来的opened去控制</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">this</span>.options.opened)&#123;</span><br><span class="line">                <span class="built_in">this</span>.$drag_modal ? <span class="built_in">this</span>.$drag_modal.style.display = <span class="string">&#x27;block&#x27;</span> : <span class="literal">null</span></span><br><span class="line">                <span class="built_in">this</span>.$drag_content ? <span class="built_in">this</span>.$drag_content.style.display = <span class="string">&#x27;block&#x27;</span> : <span class="literal">null</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h2 id="控制弹框显示隐藏的方法"><a href="#控制弹框显示隐藏的方法" class="headerlink" title="控制弹框显示隐藏的方法"></a>控制弹框显示隐藏的方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 另外 我们还需要一个方法 去打开弹框 一个方法去关闭弹框</span><br><span class="line"><span class="number">2.</span> 比如我们页面上有个按钮 我们已打开的时候 就来控制他的显示 关闭的时候 就让他隐藏</span><br><span class="line"><span class="number">3.</span> 这里我们还需要写一个销毁的方法 用来把我们创建的结构 从页面中移除</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--打开方式的调用--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn&quot;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./Dialog.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">let</span> m1 = M(&#123;</span></span><br><span class="line"><span class="javascript">            template:<span class="string">&#x27;今天我完成了插件组件的封装&#x27;</span>,</span></span><br><span class="line"><span class="javascript">            model:<span class="literal">true</span>,</span></span><br><span class="line">            buttons:[</span><br><span class="line"><span class="javascript">                &#123;<span class="attr">text</span>:<span class="string">&#x27;确定&#x27;</span>&#125;,</span></span><br><span class="line"><span class="javascript">                &#123;<span class="attr">text</span>:<span class="string">&#x27;取消&#x27;</span>&#125;</span></span><br><span class="line">            ],</span><br><span class="line"><span class="javascript">            opened:<span class="literal">false</span></span></span><br><span class="line">        &#125;)</span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.getElementById(<span class="string">&#x27;btn&#x27;</span>).onclick = open</span></span><br><span class="line"><span class="javascript">        <span class="function"><span class="keyword">function</span> <span class="title">open</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(<span class="number">111</span>)</span></span><br><span class="line">            m1.open()</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 此时我们的元素都是挂在到我们自己实例上自定义属性 所以  因为元素已经创建出来了 我们只需要 控制他的display 属性即可</span></span><br><span class="line"></span><br><span class="line">ModalPlugin.prototype = &#123;</span><br><span class="line">        <span class="title">constructor</span>:<span class="title">ModalPlugin</span>,</span><br><span class="line">        <span class="title">version</span>:&#x27;1.0.0&#x27;,</span><br><span class="line">        <span class="title">createDOM</span>:<span class="title">function</span>(<span class="params"></span>)&#123;...&#125;,</span><br><span class="line">        <span class="comment">// 打开弹框的方法</span></span><br><span class="line">        <span class="function"><span class="title">open</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.$drag_modal ? <span class="built_in">this</span>.$drag_modal.style.display = <span class="string">&#x27;block&#x27;</span> : <span class="literal">null</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content ? <span class="built_in">this</span>.$drag_content.style.display = <span class="string">&#x27;block&#x27;</span> : <span class="literal">null</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">// 关闭弹框(隐藏)</span></span><br><span class="line">        <span class="function"><span class="title">close</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.$drag_modal ? <span class="built_in">this</span>.$drag_modal.style.display = <span class="string">&#x27;none&#x27;</span> : <span class="literal">null</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content ? <span class="built_in">this</span>.$drag_content.style.display = <span class="string">&#x27;none&#x27;</span> : <span class="literal">null</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">// 销毁</span></span><br><span class="line">        <span class="function"><span class="title">destroy</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.$drag_modal ? <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_modal ):<span class="literal">null</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content ? <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_content ):<span class="literal">null</span></span><br><span class="line">        &#125;,</span><br><span class="line">        init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.createDOM()</span><br><span class="line">            <span class="built_in">this</span>.options.opened ? <span class="built_in">this</span>.open() : <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="点击处理"><a href="#点击处理" class="headerlink" title="点击处理"></a>点击处理</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们点击x 的关闭弹框 点击取消和确定分别有自己要处理的事情</span><br><span class="line"><span class="number">2.</span> 这里我们要用事件委托的方式 去写 </span><br><span class="line"><span class="number">3.</span> 这几个按钮都是在我们content 里面的  所以我们委托给我们的content的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 点击 X 的时候 关闭弹框 这里我们基于事件委托的方式 去判断点击源</span></span><br><span class="line">init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.createDOM()</span><br><span class="line">    <span class="built_in">this</span>.options.opened ? <span class="built_in">this</span>.open() : <span class="literal">null</span></span><br><span class="line">    <span class="comment">// 1. 基于事件委托 去实现不同的点击事件</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">        <span class="keyword">let</span>  _this = <span class="built_in">this</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content.addEventListener(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">            <span class="comment">// 看看 你点击的是哪个 是X 还是取消 还是 确定</span></span><br><span class="line">            <span class="keyword">let</span> target = ev.target</span><br><span class="line">                targetTag = target.tagName</span><br><span class="line">                targetClass = target.className</span><br><span class="line">            <span class="comment">// 如果点击的是x 说明就应该关闭弹框 函数不在向下执行</span></span><br><span class="line">            <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_close&#x27;</span>)&#123;</span><br><span class="line">                <span class="comment">//由于这里我们this的指向 已经改变成我们的 drag_content 所以需要 缓存一下this</span></span><br><span class="line">                _this.close()</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 点击我们的传来的buttons 按钮组 </span><br><span class="line"><span class="number">2.</span> 由于我们使用的是 事件委托的方式 方式 样式 和 标签样式 都是一样的 所以我们不知道点击的是哪个按钮</span><br><span class="line"><span class="number">3.</span> 所以我们创建 按钮的时候 直接就给我们的按钮设置一个自定义属性 赋值是我们的索引 这样 我们能拿到按钮组 在options上 就可以通过索引去选择了哪个按钮 </span><br><span class="line"><span class="number">4.</span> 点击按钮的时候 我们按钮组 传递过来了函数 我们需要让这个函数执行 另外 为了方便 我们还需要把我们的<span class="built_in">this</span>传递进去 让buttons里的函数里的<span class="built_in">this</span> 是我们的弹框的实例 如果不使用箭头函数的话 我们也以使用call 的方式 改变我们的<span class="built_in">this</span>指向</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 传递的按钮组</span></span><br><span class="line"><span class="keyword">let</span> m1 = M(&#123;</span><br><span class="line">    template:<span class="string">&#x27;今天我完成了插件组件的封装&#x27;</span>,</span><br><span class="line">    model:<span class="literal">true</span>,</span><br><span class="line">    buttons:[</span><br><span class="line">        &#123;<span class="attr">text</span>:<span class="string">&#x27;确定&#x27;</span>,<span class="attr">click</span>:<span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="built_in">this</span>)</span><br><span class="line">        &#125;&#125;,</span><br><span class="line">        &#123;<span class="attr">text</span>:<span class="string">&#x27;取消&#x27;</span>,<span class="attr">click</span>:<span class="function">(<span class="params">m</span>) =&gt;</span> &#123;</span><br><span class="line">          <span class="built_in">console</span>.log(m)</span><br><span class="line">        &#125;&#125;</span><br><span class="line">    ],</span><br><span class="line">    opened:<span class="literal">false</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建DOM的时候 设置自定索引</span></span><br><span class="line"><span class="built_in">this</span>.$drag_content.innerHTML = </span><br><span class="line">  <span class="string">`</span></span><br><span class="line"><span class="string">  &lt;div class=&quot;drag_head&quot;&gt;</span></span><br><span class="line"><span class="string">      <span class="subst">$&#123;title&#125;</span></span></span><br><span class="line"><span class="string">      &lt;a href=&quot;javascript:;&quot; class=&quot;drag_close&quot;&gt;&lt;/a&gt;</span></span><br><span class="line"><span class="string">  &lt;/div&gt;</span></span><br><span class="line"><span class="string">  &lt;div class=&quot;drag_main&quot;&gt;</span></span><br><span class="line"><span class="string">      <span class="subst">$&#123;template&#125;</span></span></span><br><span class="line"><span class="string">  &lt;/div&gt;</span></span><br><span class="line"><span class="string">  <span class="subst">$&#123;buttons.length &gt; <span class="number">0</span>? <span class="string">`&lt;div class=&quot;drag_foot&quot;&gt;</span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">      <span class="subst">$&#123;buttons.map((item,index) =&gt;	</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">  																			<span class="regexp">//</span>这里设置了自定义索引</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">          <span class="keyword">return</span> <span class="string">`&lt;a href=&quot;javascript:;&quot; index=&quot;<span class="subst">$&#123;index&#125;</span>&quot; class=&quot;drag_button&quot;&gt;</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                      <span class="subst">$&#123;item.text&#125;</span></span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                  &lt;/a&gt;`</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">      &#125;</span>).join(&#x27;&#x27;)&#125;</span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">  &lt;/div&gt;`</span>:<span class="string">&#x27;&#x27;</span>&#125;</span></span></span><br><span class="line"><span class="string">  `</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 事件委托的时候 点击我们穿进来的按钮组</span></span><br><span class="line"></span><br><span class="line"> init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.createDOM()</span><br><span class="line">            <span class="built_in">this</span>.options.opened ? <span class="built_in">this</span>.open() : <span class="literal">null</span></span><br><span class="line">            <span class="comment">//  基于事件委托 去实现不同的点击事件</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">                <span class="keyword">let</span>  _this = <span class="built_in">this</span></span><br><span class="line">                <span class="built_in">this</span>.$drag_content.addEventListener(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">                    <span class="keyword">let</span> target = ev.target</span><br><span class="line">                        targetTag = target.tagName</span><br><span class="line">                        targetClass = target.className</span><br><span class="line">                    <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_close&#x27;</span>)&#123;</span><br><span class="line">                        _this.close()</span><br><span class="line">                        <span class="keyword">return</span></span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果点击是我们的按钮 也就是我们的自定义按钮 这里如果确定 点击的是哪个按钮</span></span><br><span class="line"><span class="comment">// 我们在创建元素的时候 就给每个按钮设定自定义属性(这里我们设置的是索引) 然后 我们点击的时候 获取他的自定义属性</span></span><br><span class="line"><span class="comment">// 然后 根据索引 我们去buttons里找到这个按钮就可以确定是哪个按钮了</span></span><br><span class="line">                    <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_button&#x27;</span>)&#123;</span><br><span class="line">                        <span class="comment">// 获取我们创建的时候 设置的自定义索引</span></span><br><span class="line">                        <span class="keyword">let</span> index = target.getAttribute(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">                        <span class="comment">// 根据取到的索引 我们去查找我们的自定义按钮</span></span><br><span class="line">                            obj = _this.options.buttons[index]</span><br><span class="line">                        <span class="comment">// 做判断 如果你不存在 我就什么也不干</span></span><br><span class="line">                        <span class="keyword">if</span>(!obj || (<span class="keyword">typeof</span> obj.click !== <span class="string">&#x27;function&#x27;</span>)) <span class="keyword">return</span></span><br><span class="line">                        <span class="comment">// 这里 我们需要把函数调用 并且把this传进去</span></span><br><span class="line">                        <span class="comment">// 这里为了防止你在传 buttons 参数的时候 里面click 写的箭头函数 所以 我们把this也传进去</span></span><br><span class="line">                        obj.click.call(_this,_this)</span><br><span class="line">                        <span class="keyword">return</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h2 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这里 我们在创建的时候 应该是点击按钮 创建实例 所以 这里我们暂时先吧opened去掉</span><br><span class="line"><span class="number">2.</span> 关闭的时候 直接就销毁</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;./reset.min.css&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;./Dialog.css&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn&quot;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./Dialog.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.getElementById(<span class="string">&#x27;btn&#x27;</span>).onclick = open</span></span><br><span class="line"><span class="javascript">        <span class="function"><span class="keyword">function</span> <span class="title">open</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line">            M(&#123;</span><br><span class="line"><span class="javascript">            template:<span class="string">&#x27;今天我完成了插件组件的封装&#x27;</span>,</span></span><br><span class="line"><span class="javascript">            model:<span class="literal">true</span>,</span></span><br><span class="line">            buttons:[</span><br><span class="line"><span class="javascript">                &#123;<span class="attr">text</span>:<span class="string">&#x27;确定&#x27;</span>,<span class="attr">click</span>:<span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                    <span class="built_in">console</span>.log(<span class="built_in">this</span>)</span></span><br><span class="line">                &#125;&#125;,</span><br><span class="line"><span class="javascript">                &#123;<span class="attr">text</span>:<span class="string">&#x27;取消&#x27;</span>,<span class="attr">click</span>:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                    <span class="built_in">this</span>.close()</span></span><br><span class="line"></span><br><span class="line">                &#125;&#125;</span><br><span class="line">            ],</span><br><span class="line">        &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">ModalPlugin</span>(<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.options = options</span><br><span class="line">        <span class="built_in">this</span>.$drag_modal = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.init()</span><br><span class="line">    &#125;</span><br><span class="line">    ModalPlugin.prototype = &#123;</span><br><span class="line">        <span class="title">constructor</span>:<span class="title">ModalPlugin</span>,</span><br><span class="line">        <span class="title">version</span>:&#x27;1.0.0&#x27;,</span><br><span class="line">        //8.创建<span class="title">DOM</span>结构</span><br><span class="line">        <span class="title">createDOM</span>:<span class="title">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="comment">// 把我们的参数结构出来 用来 判断 你的哪些结构需要根据参数显示和隐藏</span></span><br><span class="line">            <span class="keyword">let</span> &#123;modal,title,template,buttons&#125; = <span class="built_in">this</span>.options</span><br><span class="line">            <span class="comment">// 这里我们注意 因为我们创建了两次元素 并且往body里插入了两次 就会产生两次回流 所以我们使用文档碎片</span></span><br><span class="line">            <span class="keyword">let</span> frag = <span class="built_in">document</span>.createDocumentFragment()</span><br><span class="line">            <span class="keyword">if</span>(modal)&#123;</span><br><span class="line">                <span class="built_in">this</span>.$drag_modal = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">                <span class="built_in">this</span>.$drag_modal.className = <span class="string">&#x27;drag_modal&#x27;</span></span><br><span class="line">                frag.appendChild(<span class="built_in">this</span>.$drag_modal)</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">this</span>.$drag_content = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">            <span class="built_in">this</span>.$drag_content.className = <span class="string">&#x27;drag_content&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content.innerHTML = <span class="string">`</span></span><br><span class="line"><span class="string">            &lt;div class=&quot;drag_head&quot;&gt;</span></span><br><span class="line"><span class="string">                <span class="subst">$&#123;title&#125;</span></span></span><br><span class="line"><span class="string">                &lt;a href=&quot;javascript:;&quot; class=&quot;drag_close&quot;&gt;&lt;/a&gt;</span></span><br><span class="line"><span class="string">            &lt;/div&gt;</span></span><br><span class="line"><span class="string">            &lt;div class=&quot;drag_main&quot;&gt;</span></span><br><span class="line"><span class="string">                <span class="subst">$&#123;template&#125;</span></span></span><br><span class="line"><span class="string">            &lt;/div&gt;</span></span><br><span class="line"><span class="string">            <span class="subst">$&#123;buttons.length &gt; <span class="number">0</span>? <span class="string">`&lt;div class=&quot;drag_foot&quot;&gt;</span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">                <span class="subst">$&#123;buttons.map((item,index) =&gt; &#123;</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">                    <span class="keyword">return</span> <span class="string">`&lt;a href=&quot;javascript:;&quot; index=&quot;<span class="subst">$&#123;index&#125;</span>&quot; class=&quot;drag_button&quot;&gt;</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                                <span class="subst">$&#123;item.text&#125;</span></span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                            &lt;/a&gt;`</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">                &#125;).join(<span class="string">&#x27;&#x27;</span>)&#125;</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">            &lt;/div&gt;`</span>:<span class="string">&#x27;&#x27;</span>&#125;</span></span></span><br><span class="line"><span class="string">            `</span></span><br><span class="line">            frag.appendChild(<span class="built_in">this</span>.$drag_content)</span><br><span class="line">            <span class="built_in">document</span>.body.appendChild(frag)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="function"><span class="title">close</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.$drag_modal ? <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_modal ):<span class="literal">null</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content ? <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_content ):<span class="literal">null</span></span><br><span class="line">        &#125;,</span><br><span class="line">        init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.createDOM()</span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">                <span class="keyword">let</span>  _this = <span class="built_in">this</span></span><br><span class="line">                <span class="built_in">this</span>.$drag_content.addEventListener(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">                    <span class="keyword">let</span> target = ev.target</span><br><span class="line">                        targetTag = target.tagName</span><br><span class="line">                        targetClass = target.className</span><br><span class="line">                    <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_close&#x27;</span>)&#123;</span><br><span class="line">                        _this.close()</span><br><span class="line">                        <span class="keyword">return</span></span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_button&#x27;</span>)&#123;</span><br><span class="line">                        <span class="keyword">let</span> index = target.getAttribute(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">                            obj = _this.options.buttons[index]</span><br><span class="line">                        <span class="keyword">if</span>(!obj || (<span class="keyword">typeof</span> obj.click !== <span class="string">&#x27;function&#x27;</span>)) <span class="keyword">return</span></span><br><span class="line">                        obj.click.call(_this,_this)</span><br><span class="line">                        <span class="keyword">return</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> isObject = <span class="function"><span class="keyword">function</span> (<span class="params">value</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> class2type = &#123;&#125;</span><br><span class="line">            type = class2type.toString.call(value)</span><br><span class="line">        <span class="keyword">return</span> <span class="regexp">/Object/</span>.test(type)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> toType = <span class="function"><span class="keyword">function</span> (<span class="params">value</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> class2type = &#123;&#125;</span><br><span class="line">            type = class2type.toString.call(value)</span><br><span class="line">        <span class="keyword">return</span>  <span class="regexp">/^\[object ([a-z-A-Z]+)\]$/</span>.exec(type)[<span class="number">1</span>].toLowerCase()</span><br><span class="line">    &#125;</span><br><span class="line">    ModalPlugin.isObject = isObject</span><br><span class="line">    ModalPlugin.toType = toType</span><br><span class="line">    <span class="keyword">const</span> props = &#123;</span><br><span class="line">        title:&#123;</span><br><span class="line">            type:<span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:<span class="string">&#x27;系统温馨提示&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        template:&#123;</span><br><span class="line">            type:<span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">            required:<span class="literal">true</span></span><br><span class="line">        &#125;,</span><br><span class="line">        buttons:&#123;</span><br><span class="line">            type:<span class="string">&#x27;array&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:[]</span><br><span class="line">        &#125;,</span><br><span class="line">        modal:&#123;</span><br><span class="line">            type:<span class="string">&#x27;boolean&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:<span class="literal">true</span></span><br><span class="line">        &#125;,</span><br><span class="line">        drag:&#123;</span><br><span class="line">            type:<span class="string">&#x27;boolean&#x27;</span>,</span><br><span class="line">            <span class="keyword">default</span>:<span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">proxy</span>(<span class="params"> options = &#123;&#125; </span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">Object</span>.prototype.toString.call(options) !== <span class="string">&#x27;[object Object]&#x27;</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">&#x27;类型不对,需要一个对象&#x27;</span>,)</span><br><span class="line">        <span class="keyword">let</span> params = &#123;&#125;</span><br><span class="line">        <span class="built_in">Object</span>.keys(props).forEach( <span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> &#123;</span><br><span class="line">                type,<span class="attr">default</span>:defaultVal,required = <span class="literal">false</span></span><br><span class="line">            &#125; = props[key]</span><br><span class="line">            <span class="keyword">let</span> val =  options[key]</span><br><span class="line">            <span class="keyword">if</span>(required &amp;&amp; val == <span class="literal">undefined</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">`<span class="subst">$&#123;key&#125;</span> 必须传递`</span>)</span><br><span class="line">            <span class="keyword">if</span>(val !== <span class="literal">undefined</span>)&#123;</span><br><span class="line">                <span class="keyword">if</span>(toType(val) !== type) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">`<span class="subst">$&#123;key&#125;</span> 不是 <span class="subst">$&#123;type&#125;</span>`</span>)</span><br><span class="line">                params[key] = val</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            params[key] = defaultVal</span><br><span class="line">        &#125;)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ModalPlugin(params)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">window</span> !== <span class="string">&#x27;undefined&#x27;</span>)&#123;</span><br><span class="line">        <span class="built_in">window</span>.M = <span class="built_in">window</span>.ModalPlugin  = proxy</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">module</span> === <span class="string">&#x27;object&#x27;</span> &amp;&amp; <span class="keyword">typeof</span> <span class="built_in">module</span>.exports === <span class="string">&#x27;object&#x27;</span>)&#123;</span><br><span class="line">        <span class="built_in">module</span>.exports = proxy</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
<h2 id="动画"><a href="#动画" class="headerlink" title="动画"></a>动画</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这里我们用过度去写动画 这里写个简单的 就是透明度的动画设置 那么初始我们的透明度设置为 <span class="number">0</span>  设置transition属性 等到 我们的元素插入到了body中 我们就把动画设置为<span class="number">1</span></span><br><span class="line"><span class="number">2.</span> 当关闭的时候 正好和打开相反</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 我们在DOM插入在body中以后 在修改透明度 为1。这个时候 动画并没有出现 因为 你初始时0 然后后来改成1 修改样式的操作 统一在我们的渲染队列里 直到在没有修改样式的操作之后 才会同意回流  这个时候指已最后的为准 所以这里 我们要获取一下 样式 刷新渲染队列 然后在设置他的透明度 为1</span></span><br><span class="line"></span><br><span class="line">init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.createDOM()</span><br><span class="line">            <span class="comment">// 如果不获取一下样式 我们初始的时候样式 透明度 是0 然后插入到DOM后直接就改成1了</span></span><br><span class="line">            <span class="comment">// 这种在修改 是一瞬间的时候 因为 获取从你的渲染队列里去拿修改的样式 所以这里 我们要获取一下 让渲染队列刷新</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_modal.offsetLeft</span><br><span class="line">            <span class="built_in">this</span>.$drag_modal.style.opacity = <span class="number">1</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content.style.opacity = <span class="number">1</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">                <span class="keyword">let</span>  _this = <span class="built_in">this</span></span><br><span class="line">                <span class="built_in">this</span>.$drag_content.addEventListener(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">                    <span class="keyword">let</span> target = ev.target</span><br><span class="line">                        targetTag = target.tagName</span><br><span class="line">                        targetClass = target.className</span><br><span class="line">                    <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_close&#x27;</span>)&#123;</span><br><span class="line">                        _this.close()</span><br><span class="line">                        <span class="keyword">return</span></span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_button&#x27;</span>)&#123;</span><br><span class="line">                        <span class="keyword">let</span> index = target.getAttribute(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">                            obj = _this.options.buttons[index]</span><br><span class="line">                        <span class="keyword">if</span>(!obj || (<span class="keyword">typeof</span> obj.click !== <span class="string">&#x27;function&#x27;</span>)) <span class="keyword">return</span></span><br><span class="line">                        obj.click.call(_this,_this)</span><br><span class="line">                        <span class="keyword">return</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 关闭的动画 </span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">close</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_modal )&#123;</span><br><span class="line">      	<span class="comment">//这样动画会没有效果 因为此时你动画还没结束 元素就被移除了</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_modal.style.opacity = <span class="number">0</span></span><br><span class="line">        <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_modal )</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">        <span class="built_in">this</span>.$drag_content.style.opacity = <span class="number">0</span></span><br><span class="line">        <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_content )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">// 这样动画才会有效果</span></span><br><span class="line"><span class="function"><span class="title">close</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_modal )&#123;</span><br><span class="line">        <span class="built_in">this</span>.$drag_modal.style.opacity = <span class="number">0</span></span><br><span class="line">        <span class="comment">//这里需要加入动画结束事件 如果不加的话 你动画还没结束的时候 </span></span><br><span class="line">        <span class="comment">//你就移除了元素 动画会导致没效果</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_modal.ontransitionend = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_modal )</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">        <span class="built_in">this</span>.$drag_content.style.opacity = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content.ontransitionend = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_content )</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="拖拽"><a href="#拖拽" class="headerlink" title="拖拽"></a>拖拽</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先 传递参数的时候 传递drag 的时候 我们才可以去多拽</span><br><span class="line"><span class="number">2.</span> 我们首先要鼠标按下 我们的头部 然后给头部绑定鼠标按下事件</span><br><span class="line"><span class="number">3.</span> 这个时候 由于我们的事件源是 我们的头部 所以为了 保证<span class="built_in">this</span>指向正确 我们要用bind</span><br><span class="line"><span class="number">4.</span> 鼠标按下的时候 我们要记录 我们鼠标按下的位置 还有 content的初始位置 这样我们要挂在到我们的实例上 </span><br><span class="line"><span class="number">5.</span> 鼠标在移动的时候 我们要拿到 我们鼠标移动的位置 然后 计算出来我们的 要拖拽的距离 用鼠标移动的距离 - 鼠标按下的距离 + content的初始位置 </span><br><span class="line"><span class="number">6.</span> 这里我们还有一个边界的问题 首先我们要拿到我们一屏的高度 和 宽度 如果你移动的距离小于我们的最小距离 那么就等于 我们的最小距离  最大距离 = 一屏的宽度 - 我们盒子自身的宽 就是最大安全距离 如果超过了 就等于 我们的最大安全距离</span><br><span class="line"><span class="number">7.</span> 这里为了 解决光标丢失的问题 我们使用windown绑定 因为使用DOM <span class="number">0</span>级 绑定 所以 解绑的时候 不用找哪个函数 直接 等于 <span class="literal">null</span> 就行了</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ModalPlugin</span>(<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.options = options</span><br><span class="line">        <span class="comment">//8.1 需要用的东西都初始化一下</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_modal = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content = <span class="literal">null</span></span><br><span class="line">        <span class="comment">//15.把鼠标的开始位置 和我们的盒子的位置 记录一下</span></span><br><span class="line">        <span class="built_in">this</span>.startX = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.startY = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.contentL = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.contentT = <span class="number">0</span></span><br><span class="line">        <span class="comment">//正平的宽度</span></span><br><span class="line">        <span class="built_in">this</span>.HTML = <span class="built_in">document</span>.documentElement</span><br><span class="line">        <span class="comment">//7.1 初始化结构  也就是创建 html结构</span></span><br><span class="line">        <span class="built_in">this</span>.init()</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 原型上的方法</span></span><br><span class="line">init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.createDOM()</span><br><span class="line">    <span class="built_in">this</span>.$drag_modal.offsetLeft</span><br><span class="line">    <span class="built_in">this</span>.$drag_modal.style.opacity = <span class="number">1</span></span><br><span class="line">    <span class="built_in">this</span>.$drag_content.style.opacity = <span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">        <span class="keyword">let</span>  _this = <span class="built_in">this</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content.addEventListener(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">let</span> target = ev.target</span><br><span class="line">                targetTag = target.tagName</span><br><span class="line">                targetClass = target.className</span><br><span class="line">            <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_close&#x27;</span>)&#123;</span><br><span class="line">                _this.close()</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(targetTag === <span class="string">&#x27;A&#x27;</span> &amp;&amp; targetClass === <span class="string">&#x27;drag_button&#x27;</span>)&#123;</span><br><span class="line">                <span class="keyword">let</span> index = target.getAttribute(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">                    obj = _this.options.buttons[index]</span><br><span class="line">                <span class="keyword">if</span>(!obj || (<span class="keyword">typeof</span> obj.click !== <span class="string">&#x27;function&#x27;</span>)) <span class="keyword">return</span></span><br><span class="line">                obj.click.call(_this,_this)</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//开启拖拽</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.options.drag)&#123;</span><br><span class="line">        <span class="comment">// 我们需要他的头部有移动 所以要设置移动的样式 然后按下点击事件</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_head = <span class="built_in">this</span>.$drag_content? <span class="built_in">this</span>.$drag_content.querySelector(<span class="string">&#x27;.drag_head&#x27;</span>) : <span class="literal">null</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_head)&#123;</span><br><span class="line">            <span class="built_in">this</span>.$drag_head.style.cursor = <span class="string">&#x27;move&#x27;</span></span><br><span class="line">            <span class="comment">// 因为我们要在鼠标按下的事件处理函数里 拿到我们的this.$drat_content 所以为了保证this指向正确我们用bind</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_head.onmousedown = <span class="built_in">this</span>.down.bind(<span class="built_in">this</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//头部按下的事件</span></span><br><span class="line">        <span class="function"><span class="title">down</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">            <span class="comment">// 鼠标按下的时候 要记录 鼠标的位置 和你 content的位置 我们把他挂在到实例上</span></span><br><span class="line">            <span class="built_in">this</span>.startX = ev.clientX</span><br><span class="line">            <span class="built_in">this</span>.startY = ev.clientY</span><br><span class="line">            <span class="comment">// 拿到我们的content的离上面 和 左边的距离</span></span><br><span class="line">            <span class="keyword">let</span>&#123;top,left&#125; = <span class="built_in">this</span>.$drag_content.getBoundingClientRect()</span><br><span class="line">            <span class="built_in">this</span>.contentL = left</span><br><span class="line">            <span class="built_in">this</span>.contentT = top</span><br><span class="line">            <span class="comment">// 然后开始移动</span></span><br><span class="line">            <span class="built_in">window</span>.onmousemove = <span class="built_in">this</span>.move.bind(<span class="built_in">this</span>)</span><br><span class="line">            <span class="built_in">window</span>.onmouseup = <span class="built_in">this</span>.up.bind(<span class="built_in">this</span>)</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//鼠标移动的事件</span></span><br><span class="line">        <span class="function"><span class="title">move</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">            <span class="comment">// 边界处理 向左的时候 如果小于我们的最小边界了 就等于 0 </span></span><br><span class="line">            <span class="comment">// 最大边界 是向右移动 最大边界 等于 我们整屏的宽度 减去 我么content的宽度</span></span><br><span class="line">            <span class="keyword">let</span> minL = <span class="number">0</span></span><br><span class="line">                minT = <span class="number">0</span></span><br><span class="line">                maxL = <span class="built_in">this</span>.HTML.clientWidth - <span class="built_in">this</span>.$drag_content.offsetWidth</span><br><span class="line">                maxT = <span class="built_in">this</span>.HTML.clientHeight - <span class="built_in">this</span>.$drag_content.offsetHeight</span><br><span class="line">            <span class="comment">// 计算移动的距离 鼠标移动的位置 - 鼠标开始的位置 + 我们盒子的距离边界的距离</span></span><br><span class="line">            <span class="keyword">let</span> curL = ev.clientX - <span class="built_in">this</span>.startX + <span class="built_in">this</span>.contentL</span><br><span class="line">                curT = ev.clientY - <span class="built_in">this</span>.startY + <span class="built_in">this</span>.contentT</span><br><span class="line">            <span class="comment">// 移动中 看看边界距离 如果向左边移动的时候 看看小于我们的安全距离不 如果小于了就等我我们的最小安全距离</span></span><br><span class="line">            <span class="comment">// 向右移动 看看 大于我们的最大安全距离不 如果大于 就等于我们的最大安全距离</span></span><br><span class="line">            curL = curL &lt; minL ? minL : curL &gt; maxL ? maxL : curL</span><br><span class="line">            curT = curT &lt; minT ? minT : curT &gt; maxT ? maxT : curT</span><br><span class="line">            <span class="comment">// 这里由于 我们盒子居中 用的是transform 设置的 起初就偏移了了所以我们要设置一下</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content.style.transform = <span class="string">&#x27;translate(0, 0)&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content.style.left = curL + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.$drag_content.style.top = curT + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">            </span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//鼠标抬起的事件</span></span><br><span class="line">        <span class="function"><span class="title">up</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="comment">// 由于这里我们采用的DOM 0级别 绑定 所以直接把事件给null 就可以了</span></span><br><span class="line">            <span class="built_in">window</span>.onmousemove = <span class="literal">null</span></span><br><span class="line">            <span class="built_in">window</span>.onmouseup = <span class="literal">null</span></span><br><span class="line">        &#125;,</span><br></pre></td></tr></table></figure>
<h2 id="生命周期函数"><a href="#生命周期函数" class="headerlink" title="生命周期函数"></a>生命周期函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们这里主要想实现的生命周期函数有 </span><br><span class="line">+ 打开的时候 onopen</span><br><span class="line">+ 关闭的时候 onclose</span><br><span class="line">+ 拖拽开始 ondragstart</span><br><span class="line">+ 拖拽中 ondraging</span><br><span class="line">+ 拖拽结束 ondragend</span><br><span class="line"><span class="number">2.</span> 这里实现的方式 有 回到函数的形式 就在在某个阶段 调用你传进来的函数 另外一种是事件发布订阅的形式</span><br><span class="line"><span class="number">3.</span> 这里我们 onopen 和 onclose 用回调函数的形式实现 其余的用发布订阅的形式实现</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 打开的时候 我们直接 在创建元素阶段 然后调用你你传进来的函数即可 由于这两个函数是我们 后加入的 所以 我们可以直接在我的 校验的映射表添加即可 这也是 我们校验映射表的好处</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> props = &#123;</span><br><span class="line">title:&#123;</span><br><span class="line">    type:<span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">    <span class="keyword">default</span>:<span class="string">&#x27;系统温馨提示&#x27;</span></span><br><span class="line">&#125;,</span><br><span class="line">template:&#123;</span><br><span class="line">    type:<span class="string">&#x27;string&#x27;</span>,</span><br><span class="line">    required:<span class="literal">true</span></span><br><span class="line">&#125;,</span><br><span class="line">buttons:&#123;</span><br><span class="line">    type:<span class="string">&#x27;array&#x27;</span>,</span><br><span class="line">    <span class="keyword">default</span>:[]</span><br><span class="line">&#125;,</span><br><span class="line">modal:&#123;</span><br><span class="line">    type:<span class="string">&#x27;boolean&#x27;</span>,</span><br><span class="line">    <span class="keyword">default</span>:<span class="literal">true</span></span><br><span class="line">&#125;,</span><br><span class="line">drag:&#123;</span><br><span class="line">    type:<span class="string">&#x27;boolean&#x27;</span>,</span><br><span class="line">    <span class="keyword">default</span>:<span class="literal">true</span></span><br><span class="line">&#125;,</span><br><span class="line">onopen:&#123;</span><br><span class="line">    type:<span class="string">&#x27;function&#x27;</span>,</span><br><span class="line">    <span class="comment">// 如果没传我们就给个默认匿名函数</span></span><br><span class="line">    <span class="keyword">default</span>: <span class="built_in">Function</span>.prototype</span><br><span class="line">&#125;,</span><br><span class="line">onclose:&#123;</span><br><span class="line">    type:<span class="string">&#x27;function&#x27;</span>,</span><br><span class="line">    <span class="comment">// 如果没传我们就给个默认匿名函数</span></span><br><span class="line">    <span class="keyword">default</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 我们在创建DOM 和销毁的时候 直接调用这两个函数即可</span></span><br><span class="line">createDOM:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    .......很多代码</span><br><span class="line">    <span class="comment">// DOM 创建的时候 我们就调用这个open的函数 </span></span><br><span class="line">    <span class="built_in">this</span>.options.onopen.call(<span class="built_in">this</span>,<span class="built_in">this</span>)</span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">// 11.关闭弹框(隐藏)</span></span><br><span class="line"><span class="function"><span class="title">close</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  	.......很多代码</span><br><span class="line">    <span class="comment">//DOM 销毁的时候 我们就调用这个close的函数 </span></span><br><span class="line">    <span class="built_in">this</span>.options.onclose.call(<span class="built_in">this</span>,<span class="built_in">this</span>)</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="发布订阅模式实现生命周期函数"><a href="#发布订阅模式实现生命周期函数" class="headerlink" title="发布订阅模式实现生命周期函数"></a>发布订阅模式实现生命周期函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 发布订阅模式 首先我们需要一个线程池 然后on 的时候 往里边 进函数 fire的时候循环调用函数</span><br><span class="line"><span class="number">2.</span> 我们以前写过一个 这里 我们只需要把他继承过来</span><br><span class="line"><span class="number">3.</span> 这里记住 我们可以直接 把我们写的发布订阅类的 当作一个实例 挂在到我们的 弹框实例的一个属性上</span><br><span class="line"><span class="number">4.</span> 这里也可以使用即成的方式 这里我们需要使用寄生组合即成 但是我们在继承实例的线程池的时候 要把发布订阅的类 当作普通函数执行。这里 如果都是<span class="class"><span class="keyword">class</span> 写的 直接可以使用 <span class="title">super</span> + <span class="keyword">extends</span> 继承 这里要记住 <span class="title">class</span>    的类不能当作普通函数执行</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//发布订阅 类</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">Sub</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="built_in">this</span>.pond = &#123;&#125;;</span><br><span class="line">  &#125;</span><br><span class="line">  Sub.prototype = &#123;</span><br><span class="line">      <span class="title">constructor</span>: <span class="title">Sub</span>,</span><br><span class="line">      <span class="title">on</span>(<span class="params">type, func</span>) &#123;</span><br><span class="line">          <span class="keyword">let</span> pond = <span class="built_in">this</span>.pond;</span><br><span class="line">          !pond.hasOwnProperty(type) ? pond[type] = [] : <span class="literal">null</span>;</span><br><span class="line">          <span class="keyword">let</span> arr = pond[type];</span><br><span class="line">          <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">              <span class="keyword">if</span> (arr[i] === func) &#123;</span><br><span class="line">                  <span class="keyword">return</span>;</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line">          arr.push(func);</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="function"><span class="title">off</span>(<span class="params">type, func</span>)</span> &#123;</span><br><span class="line">          <span class="keyword">let</span> arr = <span class="built_in">this</span>.pond[type] || [];</span><br><span class="line">          <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">              <span class="keyword">if</span> (arr[i] === func) &#123;</span><br><span class="line">                  arr[i] = <span class="literal">null</span>;</span><br><span class="line">                  <span class="keyword">break</span>;</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="function"><span class="title">fire</span>(<span class="params">type, ...params</span>)</span> &#123;</span><br><span class="line">          <span class="keyword">let</span> arr = <span class="built_in">this</span>.pond[type] || [];</span><br><span class="line">          <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">              <span class="keyword">let</span> item = arr[i];</span><br><span class="line">              <span class="keyword">if</span> (<span class="keyword">typeof</span> item === <span class="string">&quot;function&quot;</span>) &#123;</span><br><span class="line">                  item.call(<span class="built_in">this</span>, ...params);</span><br><span class="line">                  <span class="keyword">continue</span>;</span><br><span class="line">              &#125;</span><br><span class="line">              arr.splice(i, <span class="number">1</span>);</span><br><span class="line">              i--;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">  <span class="built_in">window</span>.Sub = Sub;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 开始继承。 首先要把Sub当作 普通函数 在我们的 Dialog 类里用call 执行</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ModalPlugin</span>(<span class="params">options</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//开始我们的继承组合继承 首先继承我们的线程池 当作一个实例</span></span><br><span class="line">        Sub.call(<span class="built_in">this</span>)</span><br><span class="line">        <span class="built_in">this</span>.options = options</span><br><span class="line">        <span class="built_in">this</span>.$drag_modal = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.startX = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.startY = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.contentL = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.contentT = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.HTML = <span class="built_in">document</span>.documentElement</span><br><span class="line">        <span class="built_in">this</span>.init()</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 继承Sub 原型上的方法首先我们让我们 Dialog 类的 原型prototype = 空对象 在让这个空对象的 原型链__proto__ 指向我们的 Sub 类的 原型 prototype  当然我们可以是用 Obejct.create()</span></span><br><span class="line"><span class="comment">// 然后 在把我们Dialog的类prototype 方法 合并一下</span></span><br><span class="line"></span><br><span class="line">ModalPlugin.prototype = <span class="built_in">Object</span>.create(Sub.prototype)</span><br><span class="line">ModalPlugin.prototype = <span class="built_in">Object</span>.assign(ModalPlugin.prototype,</span><br><span class="line">&#123;</span><br><span class="line"><span class="title">constructor</span>:<span class="title">ModalPlugin</span>,</span><br><span class="line"><span class="title">version</span>:&#x27;1.0.0&#x27;,</span><br><span class="line"><span class="title">createDOM</span>:<span class="title">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;modal,title,template,buttons&#125; = <span class="built_in">this</span>.options</span><br><span class="line">    <span class="keyword">let</span> frag = <span class="built_in">document</span>.createDocumentFragment()</span><br><span class="line">    <span class="keyword">if</span>(modal)&#123;</span><br><span class="line">        <span class="built_in">this</span>.$drag_modal = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">        <span class="built_in">this</span>.$drag_modal.className = <span class="string">&#x27;drag_modal&#x27;</span></span><br><span class="line">        frag.appendChild(<span class="built_in">this</span>.$drag_modal)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">this</span>.$drag_content = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">    <span class="built_in">this</span>.$drag_content.className = <span class="string">&#x27;drag_content&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.$drag_content.innerHTML = <span class="string">`</span></span><br><span class="line"><span class="string">    &lt;div class=&quot;drag_head&quot;&gt;</span></span><br><span class="line"><span class="string">        <span class="subst">$&#123;title&#125;</span></span></span><br><span class="line"><span class="string">        &lt;a href=&quot;javascript:;&quot; class=&quot;drag_close&quot;&gt;&lt;/a&gt;</span></span><br><span class="line"><span class="string">    &lt;/div&gt;</span></span><br><span class="line"><span class="string">    &lt;div class=&quot;drag_main&quot;&gt;</span></span><br><span class="line"><span class="string">        <span class="subst">$&#123;template&#125;</span></span></span><br><span class="line"><span class="string">    &lt;/div&gt;</span></span><br><span class="line"><span class="string">    <span class="subst">$&#123;buttons.length &gt; <span class="number">0</span>? <span class="string">`&lt;div class=&quot;drag_foot&quot;&gt;</span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">        <span class="subst">$&#123;buttons.map((item,index) =&gt; &#123;</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">            <span class="keyword">return</span> <span class="string">`&lt;a href=&quot;javascript:;&quot; index=&quot;<span class="subst">$&#123;index&#125;</span>&quot; class=&quot;drag_button&quot;&gt;</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                        <span class="subst">$&#123;item.text&#125;</span></span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst"><span class="string">                    &lt;/a&gt;`</span></span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string"><span class="subst">        &#125;).join(<span class="string">&#x27;&#x27;</span>)&#125;</span></span></span></span></span><br><span class="line"><span class="string"><span class="subst"><span class="string">    &lt;/div&gt;`</span>:<span class="string">&#x27;&#x27;</span>&#125;</span></span></span><br><span class="line"><span class="string">    `</span></span><br><span class="line">    frag.appendChild(<span class="built_in">this</span>.$drag_content)</span><br><span class="line">    <span class="built_in">document</span>.body.appendChild(frag)</span><br><span class="line">    <span class="built_in">this</span>.options.onopen.call(<span class="built_in">this</span>,<span class="built_in">this</span>)</span><br><span class="line">&#125;,</span><br><span class="line"><span class="function"><span class="title">close</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_modal )&#123;</span><br><span class="line">        <span class="built_in">this</span>.$drag_modal.style.opacity = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_modal.ontransitionend = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_modal )</span><br><span class="line">            <span class="built_in">this</span>.$drag_modal.ontransitionend = <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$drag_content)&#123;</span><br><span class="line">        <span class="built_in">this</span>.$drag_content.style.opacity = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.$drag_content.ontransitionend = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">document</span>.body.removeChild( <span class="built_in">this</span>.$drag_content )</span><br><span class="line">            <span class="built_in">this</span>.$drag_content.ontransitionend = <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="built_in">this</span>.options.onclose.call(<span class="built_in">this</span>,<span class="built_in">this</span>)</span><br><span class="line">&#125;,</span><br><span class="line"><span class="function"><span class="title">down</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.startX = ev.clientX</span><br><span class="line">    <span class="built_in">this</span>.startY = ev.clientY</span><br><span class="line">    <span class="keyword">let</span>&#123;top,left&#125; = <span class="built_in">this</span>.$drag_content.getBoundingClientRect()</span><br><span class="line">    <span class="built_in">this</span>.contentL = left</span><br><span class="line">    <span class="built_in">this</span>.contentT = top</span><br><span class="line">    <span class="built_in">window</span>.onmousemove = <span class="built_in">this</span>.move.bind(<span class="built_in">this</span>)</span><br><span class="line">    <span class="built_in">window</span>.onmouseup = <span class="built_in">this</span>.up.bind(<span class="built_in">this</span>)</span><br><span class="line">		 <span class="comment">// 22. 通知 我们拖拽开始 可以执行的函数 把this传入进去</span></span><br><span class="line">    <span class="built_in">this</span>.fire(<span class="string">&#x27;ondragstart&#x27;</span>, <span class="built_in">this</span>);</span><br><span class="line">&#125;,</span><br><span class="line"><span class="function"><span class="title">move</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> minL = <span class="number">0</span></span><br><span class="line">        minT = <span class="number">0</span></span><br><span class="line">        maxL = <span class="built_in">this</span>.HTML.clientWidth - <span class="built_in">this</span>.$drag_content.offsetWidth</span><br><span class="line">        maxT = <span class="built_in">this</span>.HTML.clientHeight - <span class="built_in">this</span>.$drag_content.offsetHeight</span><br><span class="line">    <span class="keyword">let</span> curL = ev.clientX - <span class="built_in">this</span>.startX + <span class="built_in">this</span>.contentL</span><br><span class="line">        curT = ev.clientY - <span class="built_in">this</span>.startY + <span class="built_in">this</span>.contentT</span><br><span class="line">    curL = curL &lt; minL ? minL : curL &gt; maxL ? maxL : curL</span><br><span class="line">    curT = curT &lt; minT ? minT : curT &gt; maxT ? maxT : curT</span><br><span class="line">    <span class="built_in">this</span>.$drag_content.style.transform = <span class="string">&#x27;translate(0, 0)&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.$drag_content.style.left = curL + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">    <span class="built_in">this</span>.$drag_content.style.top = curT + <span class="string">&#x27;px&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 22. 通知 我们拖拽中 可以执行的函数</span></span><br><span class="line">    <span class="built_in">this</span>.fire(<span class="string">&#x27;ondraging&#x27;</span>, <span class="built_in">this</span>);</span><br><span class="line">&#125;,</span><br><span class="line"><span class="function"><span class="title">up</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">window</span>.onmousemove = <span class="literal">null</span></span><br><span class="line">    <span class="built_in">window</span>.onmouseup = <span class="literal">null</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 22. 拖拽结束的时候通知执行的函数</span></span><br><span class="line">    <span class="built_in">this</span>.fire(<span class="string">&#x27;ondragend&#x27;</span>, <span class="built_in">this</span>);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 调用</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">&#x27;btn&#x27;</span>).onclick = open</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">open</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> <span class="keyword">let</span> m1 =  M(&#123;</span><br><span class="line">    template:<span class="string">&#x27;今天我完成了插件组件的封装&#x27;</span>,</span><br><span class="line">    model:<span class="literal">true</span>,</span><br><span class="line">    buttons:[</span><br><span class="line">        &#123;<span class="attr">text</span>:<span class="string">&#x27;确定&#x27;</span>,<span class="attr">click</span>:<span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="built_in">this</span>)</span><br><span class="line">        &#125;&#125;,</span><br><span class="line">        &#123;<span class="attr">text</span>:<span class="string">&#x27;取消&#x27;</span>,<span class="attr">click</span>:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.close()</span><br><span class="line"></span><br><span class="line">        &#125;&#125;</span><br><span class="line">    ],</span><br><span class="line">    drag:<span class="literal">true</span>,</span><br><span class="line">    onopen:<span class="function"><span class="keyword">function</span> (<span class="params">m</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>,m)</span><br><span class="line">    &#125;,</span><br><span class="line">    onclose:<span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>,m)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">m1.on(<span class="string">&#x27;ondragstart&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;拖拽开始&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">m1.on(<span class="string">&#x27;ondraging&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;拖拽中执行的函数&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">m1.on(<span class="string">&#x27;ondragend&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;拖拽结束&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">地址</span><br><span class="line">https:<span class="comment">//github.com/wazer1987/Dialog</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>弹框组件</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/03/29/Dialog/">http://example.com/2020/03/29/Dialog/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-03-29</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-04</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaScript/">JavaScript</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/01/%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">事件绑定</div></div></a></div><div class="next-post pull-right"><a href="/2020/03/28/%E6%8B%96%E6%8B%BD/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530724&amp;t=333c5056a813a542419e5b661ebcfcd9" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">拖拽</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/04/27/写一个简版的Promise/" title="手写一个简版的promise"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530599&t=32fb97bad7007bdc6c712ccadacd5721" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-27</div><div class="title">手写一个简版的promise</div></div></a></div><div><a href="/2020/04/27/设计模式/" title="发布订阅"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-27</div><div class="title">发布订阅</div></div></a></div><div><a href="/2020/04/26/js事件循环机制/" title="JavaScript 事件循环机制"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-26</div><div class="title">JavaScript 事件循环机制</div></div></a></div><div><a href="/2020/04/22/ES6/" title="ES6/ES7/ES8/ES9/ES10/ES11"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-22</div><div class="title">ES6/ES7/ES8/ES9/ES10/ES11</div></div></a></div><div><a href="/2020/04/01/事件绑定/" title="事件绑定"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-01</div><div class="title">事件绑定</div></div></a></div><div><a href="/2020/03/28/拖拽/" title="拖拽"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530724&t=333c5056a813a542419e5b661ebcfcd9" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-28</div><div class="title">拖拽</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%BC%B9%E6%A1%86%E7%BB%84%E4%BB%B6%E5%B0%81%E8%A3%85"><span class="toc-text">弹框组件封装</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%9D%E6%83%B3"><span class="toc-text">思想</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9C%80%E8%A6%81%E8%80%83%E8%99%91%E7%9A%84%E4%BA%8B%E6%83%85"><span class="toc-text">需要考虑的事情</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E6%94%AF%E6%8C%81%E7%9A%84%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF"><span class="toc-text">插件支持的配置信息</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E7%9A%84%E6%96%B9%E5%BC%8F"><span class="toc-text">创建的方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92"><span class="toc-text">参数传递</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9D%E5%A7%8BDOM%E7%BB%93%E6%9E%84"><span class="toc-text">初始DOM结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%BB%98%E8%AE%A4%E6%89%93%E5%BC%80%E5%BC%B9%E6%A1%86"><span class="toc-text">默认打开弹框</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8E%A7%E5%88%B6%E5%BC%B9%E6%A1%86%E6%98%BE%E7%A4%BA%E9%9A%90%E8%97%8F%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">控制弹框显示隐藏的方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%82%B9%E5%87%BB%E5%A4%84%E7%90%86"><span class="toc-text">点击处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9"><span class="toc-text">修改</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E7%94%BB"><span class="toc-text">动画</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%96%E6%8B%BD"><span class="toc-text">拖拽</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%87%BD%E6%95%B0"><span class="toc-text">生命周期函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85%E6%A8%A1%E5%BC%8F%E5%AE%9E%E7%8E%B0%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%87%BD%E6%95%B0"><span class="toc-text">发布订阅模式实现生命周期函数</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>